﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Collections;
using System.Web.Services;
using System.Text;
using Catering_Rules;

namespace Catering.Catering_Page.Caipin.Caipin_jcsz
{
    public partial class CaipinshezhiInfo : System.Web.UI.Page
    {
        private DataTable dt_DishType = new DataTable();//菜品类别数据列表
        T_Dish tDish = new T_Dish(); //菜品管理

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadData();
                Session["dt_yuanliao" + Session["UserID"].ToString()] = new Catering_DataSet.RawInfoDataTable();
                if (Request.QueryString["cid"] != null)
                {
                    this.btn_Save_Dish.Visible = true;
                    this.bt_zengjia.Visible = false;
                    this.LoadDish();
                }
                else
                {
                    this.btn_Save_Dish.Visible = false;
                    this.bt_zengjia.Visible = true;
                }
            }
        }

        //绑定页面数据
        public void LoadData()
        {
            dt_DishType = new Catering_Rules.DishType().GetAllList(false).Tables[0];
            this.Dr_DishType.Items.Clear();
            DataRow[] RowList = dt_DishType.Select("ParentID=0");
            this.LoadDrop(RowList);
        }

        private void LoadDrop(DataRow[] RowList)
        {
            foreach (DataRow row in RowList)
            {
                ListItem ite = new ListItem();
                string Temp = "";
                int Level = Convert.ToInt32(row["TypeLevel"].ToString());
                if (Level > 0)
                {
                    for (int i = 0; i < Level; i++)
                    {
                        Temp += "┣";
                    }
                }
                ite.Text = Temp + row["DT"].ToString();
                ite.Value = row["id"].ToString();
                this.Dr_DishType.Items.Add(ite);
                DataRow[] TempList = dt_DishType.Select("ParentID="+row["id"].ToString());
                if (TempList != null && TempList.Count() > 0)
                {
                    this.LoadDrop(TempList);
                }
            }   
        }

        //选择菜
        protected void LoadDish()
        {
            string DishID = Request.QueryString["cid"].ToString();
            Catering_Model.E_Dish md_Dish = new Catering_Rules.T_Dish().GetModel(Convert.ToInt32(DishID));
            this.Txt_caiming.Text = md_Dish.Name;
            this.Txt_tedian.Text = md_Dish.Characteristic;
            this.Txt_zuofa.Text = md_Dish.Method;
            this.Dr_DishType.SelectedValue = md_Dish.DTID.ToString();

            this.ImgUpPoto.ImageUrl = md_Dish.Picture;
            if (md_Dish.IsStuffing != null && md_Dish.IsStuffing.ToString() == "1") this.Cob_YouXian.Checked = true;

            Session["dt_yuanliao" + Session["UserID"].ToString()] = new Catering_DataSet.RawInfoDataTable();
            int i = 0;
            DataTable dt_DishInfo = new Catering_Rules.T_DishInfo().GetList("DID=" + DishID).Tables[0];
            Catering.View_Operaction.Caipin_Manage_Operaction cmo = new View_Operaction.Caipin_Manage_Operaction();
            foreach (DataRow row in dt_DishInfo.Rows)
            {
                Catering_Model.E_RawMaterial md_Raw = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(row["RawID"].ToString()));
                if (md_Raw != null)
                {
                    i += 1;
                    int typeid = md_Raw.RAWIDS;
                    Catering_Model.RawMaterialsInfo md_RawInfo = new Catering_Rules.RawMaterialsInfo().GetModel(typeid);
                    string typename = md_RawInfo != null ? md_RawInfo.Name : "";
                    string strRe = new Catering_Rules.NutrientComposition().GetList("RawID=" + row["RawID"].ToString() + " and NutritionElementsID=1").Tables[0].Rows[0]["Content"].ToString();
                    Session["dt_yuanliao" + Session["UserID"].ToString()] = cmo.setDataToDataTable((DataTable)Session["dt_yuanliao" + Session["UserID"].ToString()], i, row["DName"].ToString(), typename, strRe, row["RawID"].ToString(), row["Content"].ToString() == "" ? "0" : row["Content"].ToString());
                }
            }

            SetSumNumber();
            this.SelReList();
        }

        //修改菜品
        protected void btn_Save_Dish_Click(object sender, EventArgs e)
        {
            DataTable dt_yuanliao = (DataTable)Session["dt_yuanliao" + Session["UserID"].ToString()];
            int DishID = Convert.ToInt32(Request.QueryString["cid"].ToString());
            //插入菜信息
            Catering_Model.E_Dish md_dish = new Catering_Model.E_Dish();
            md_dish.id = DishID;
            md_dish.Name = Txt_caiming.Text.Trim();
            md_dish.Characteristic = Txt_tedian.Text.Trim();
            md_dish.DTID = Convert.ToInt32(this.Dr_DishType.SelectedValue);
            md_dish.Method = Txt_zuofa.Text.Trim();
            md_dish.Back = "";
            md_dish.Picture = this.ImgUpPoto.ImageUrl;
            md_dish.IsStuffing = 0;
            if (this.Cob_YouXian.Checked) md_dish.IsStuffing = 1;
            new Catering_Rules.T_Dish().Update(md_dish);

            // 插入菜品食物信息
            new Catering_Rules.T_DishInfo().DeleteListBywhere("DID=" + DishID);
            if (dt_yuanliao != null && dt_yuanliao.Rows.Count > 0)
            {
                for (int i = 0; i < dt_yuanliao.Rows.Count; i++)
                {
                    Catering_Model.E_DishInfo md_DishInfo = new Catering_Model.E_DishInfo();
                    md_DishInfo.id = new Catering_Rules.T_DishInfo().GetMaxId();
                    md_DishInfo.DID = DishID;
                    md_DishInfo.DName = dt_yuanliao.Rows[i]["Food"].ToString();
                    md_DishInfo.RawID = Convert.ToInt32(dt_yuanliao.Rows[i]["RawID"].ToString());
                    md_DishInfo.Re = 0;//int.Parse(dt_yuanliao.Rows[i]["re"].ToString());
                    md_DishInfo.ZS = dt_yuanliao.Rows[i]["zs"].ToString();
                    md_DishInfo.Content = dt_yuanliao.Rows[i]["zl"].ToString();
                    new Catering_Rules.T_DishInfo().Add(md_DishInfo);
                }
            }

            //查询菜热量表数据
            new Catering_Rules.DishNutrien().DeleteListBywhere("Dishid=" + DishID);
            for (int i = 1; i <= 24; i++)
            {
                Catering_Model.DishNutrien md_DN = new Catering_Model.DishNutrien();
                md_DN.Dishid = DishID;
                md_DN.element_name = new Catering_Rules.NutritionElements().GetModel(i).Elements;
                md_DN.Content = dt_Dish_Re.Rows[0]["Y_" + i.ToString()].ToString();
                md_DN.fatherid = i.ToString();
                new Catering_Rules.DishNutrien().Add(md_DN);
            }

            tDish.UpdateAreaIDByDishID(DishID);//更新所属 区域IDS
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！');window.parent.SetDishName('" + DishID + "','" + Txt_caiming.Text.Trim() + "');</script>");
        }

        //增加菜品
        protected void bt_zengjia_Click(object sender, ImageClickEventArgs e)
        {
            DataTable dt_yuanliao = (DataTable)Session["dt_yuanliao" + Session["UserID"].ToString()];
            //插入菜信息
            Catering_Model.E_Dish Mdh = new Catering_Model.E_Dish();
            Mdh.Name = Txt_caiming.Text.Trim();
            Mdh.Characteristic = Txt_tedian.Text.Trim();
            Mdh.DTID = Convert.ToInt32(this.Dr_DishType.SelectedValue);
            Mdh.Method = Txt_zuofa.Text.Trim();
            Mdh.Back = "";
            Mdh.Picture = this.ImgUpPoto.ImageUrl;
            Mdh.IsStuffing = 0;
            if (this.Cob_YouXian.Checked) Mdh.IsStuffing = 1;
            int DDID = new Catering_Rules.T_Dish().Add(Mdh);

            // 插入菜品食物信息
            if (dt_yuanliao != null && dt_yuanliao.Rows.Count > 0)
            {
                for (int i = 0; i < dt_yuanliao.Rows.Count; i++)
                {
                    if (dt_yuanliao.Rows[i]["id"] != null)
                    {
                        Catering_Model.E_DishInfo _ModelDishInfo = new Catering_Model.E_DishInfo();
                        _ModelDishInfo.id = new Catering_Rules.T_DishInfo().GetMaxId();
                        _ModelDishInfo.DID = DDID;
                        _ModelDishInfo.DName = dt_yuanliao.Rows[i]["Food"].ToString();
                        _ModelDishInfo.RawID = Convert.ToInt32(dt_yuanliao.Rows[i]["RawID"].ToString());
                        _ModelDishInfo.Re = 0;//int.Parse(dt_yuanliao.Rows[i]["re"].ToString());
                        _ModelDishInfo.ZS = dt_yuanliao.Rows[i]["zs"].ToString();
                        _ModelDishInfo.Content = dt_yuanliao.Rows[i]["zl"].ToString();
                        new Catering_Rules.T_DishInfo().Add(_ModelDishInfo);
                    }
                }
            }

            for (int i = 1; i <= 24; i++)
            {
                Catering_Model.DishNutrien md_DN = new Catering_Model.DishNutrien();
                md_DN.Dishid = DDID;
                md_DN.element_name = new Catering_Rules.NutritionElements().GetModel(i).Elements;
                md_DN.Content = dt_Dish_Re.Rows[0]["Y_" + i.ToString()].ToString();
                md_DN.fatherid = i.ToString();
                new Catering_Rules.DishNutrien().Add(md_DN);
            }
            string IsTypeInfo = "false";
            string TypeID = this.Dr_DishType.SelectedValue;
            string RawID = DDID.ToString();
            string RawName = Txt_caiming.Text.Trim();
            tDish.UpdateAreaIDByDishID(DDID);//更新所属 区域IDS
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>ShowDiv();alert('保存成功！');window.parent.AddDishName('" + IsTypeInfo + "', '" + TypeID + "', '" + RawID + "', '" + RawName + "');</script>");

        }

        //上传图片
        protected void butUpClose_Click(object sender, EventArgs e)
        {
            ImgUpPoto.ImageUrl = new View_Operaction.Caipin_Manage_Operaction().UpImage(this.File_Upload);
        }

        //清空界面
        protected void ButClear_Dish_Click(object sender, EventArgs e)
        {
            this.Txt_caiming.Text = "";
            this.Txt_tedian.Text = "";
            this.Txt_zuofa.Text = "";
            Session["dt_yuanliao" + Session["UserID"].ToString()] = new Catering_DataSet.RawInfoDataTable();

            foreach (Control c in this.Pan_NE.Controls)
            {
                Label lb = c as Label;
                if (lb != null)
                {
                    lb.Text = string.Empty;
                }
            }
        }

        public static DataTable dt_Dish_Re;
        //获取热量总和
        public static void SetSumNumber()
        {
            DataTable dt_yuanliao = (DataTable)HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()];
            LoadDT_Dish_Re();//重组菜热量表
            foreach (DataRow row in dt_yuanliao.Rows)
            {
                string RawID = row["RawID"].ToString();//原料ID
                int num = Convert.ToInt32(row["zl"].ToString());//使用量
                DataTable dt_NutrientComposition = new Catering_Rules.NutrientComposition().GetList(24, "RawID=" + RawID, "NutritionElementsID").Tables[0];
                for (int i = 0; i < dt_NutrientComposition.Rows.Count; i++)
                {
                    if (dt_NutrientComposition.Rows[i]["Content"].ToString() != "")
                    {
                        dt_Dish_Re.Rows[0]["Y_" + (i + 1).ToString()] = Convert.ToDecimal(dt_Dish_Re.Rows[0]["Y_" + (i + 1).ToString()]) + (Convert.ToDecimal(dt_NutrientComposition.Rows[i]["Content"].ToString()) / 100) * num;
                    }
                }
            }
        }
        //初始化元素值表
        public static void LoadDT_Dish_Re()
        {
            dt_Dish_Re = new DataTable();
            dt_Dish_Re.Columns.Add("DishID");//菜ID
            for (int i = 1; i <= 24; i++)
            {
                dt_Dish_Re.Columns.Add("Y_" + i.ToString());
            }

            DataRow row = dt_Dish_Re.NewRow();
            row["DishID"] = "";
            for (int i = 1; i <= 24; i++)
            {
                row["Y_" + i.ToString()] = "0";
            }
            dt_Dish_Re.Rows.Add(row);

        }

        //获取原料热量
        [WebMethod]
        public static ArrayList RawNutrient(string Txt_NumberID, string Nunmber)
        {
            DataTable dt_yuanliao = (DataTable)HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()];
            ArrayList al = new ArrayList();
            dt_yuanliao.Select("RawID='" + Txt_NumberID + "'")[0]["zl"] = Convert.ToInt32(Nunmber).ToString();
            HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()] = dt_yuanliao;
            SetSumNumber();
            for (int i = 1; i <= 24; i++)
            {
                al.Add(dt_Dish_Re.Rows[0]["Y_" + i.ToString()].ToString());
            }
            return al;
        }

        //删除原料
        [WebMethod]
        public static ArrayList DelRawByRawID(string RawID)
        {
            DataTable dt_yuanliao = (DataTable)HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()];
            ArrayList al = new ArrayList();
            dt_yuanliao.Select("RawID='" + RawID + "'")[0].Delete();
            HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()] = dt_yuanliao;
            al.Add("OK");
            SetSumNumber();
            for (int i = 1; i <= 24; i++)
            {
                al.Add(dt_Dish_Re.Rows[0]["Y_" + i.ToString()].ToString());
            }
            return al;
        }

        //添加原料
        [WebMethod]
        public static ArrayList AddRawByRawID(string RawID)
        {
            DataTable dt_yuanliao = (DataTable)HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()];
            ArrayList al = new ArrayList();
            Catering_Model.E_RawMaterial cmr = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(RawID));
            string strRe = new Catering_Rules.NutrientComposition().GetList("RawID=" + RawID + " and NutritionElementsID=1").Tables[0].Rows[0]["Content"].ToString();
            Catering_Model.E_RawMaterial md_RM = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(RawID));
            string strTypeName = new Catering_Rules.RawMaterialsInfo().GetModel(md_RM.RAWIDS).Name;

            if (dt_yuanliao.Select("RawID=" + RawID).Count() < 1)
            {
                al.Add("OK");
                //StringBuilder sb = new StringBuilder();
                //sb.Append("<tr id=\"yuanliao_List_"+RawID+"\">");
                //sb.Append("<td class='xh_td'>&nbsp;</td>");
                //sb.Append("<td id='RawName'>" + cmr.RAWName + "</td>");
                //sb.Append("<td>" + strTypeName + "</td>");
                //sb.Append("<td id='leibie'><input type='text' RawID=\"" + RawID + "\" value='100' id='Txt_jinghanliang" + RawID + "' style='width: 50px'/></td>");
                //sb.Append("<td>" + strRe + "</td>");
                //sb.Append("<td align='center'><img src=\"../../../Images/trash.gif\" onclick=\"DelRaw('" + RawID + "')\" /></td>");
                //sb.Append("</tr> ");
                //al.Add(sb.ToString());
                HttpContext.Current.Session["dt_yuanliao" + HttpContext.Current.Session["UserID"].ToString()] = new View_Operaction.Caipin_Manage_Operaction().setDataToDataTable(dt_yuanliao, 0, cmr.RAWName, strTypeName, strRe, RawID);

                //for (int i = 1; i <= 24; i++)
                //{
                //    al.Add(dt_Dish_Re.Rows[0]["Y_" + i.ToString()].ToString());
                //}
            }
            else
            {
                al.Add("NO");
            }
            return al;
        }

        //给页面元素赋值
        public void SelReList()
        {
            foreach (Control c in this.Pan_NE.Controls)
            {
                Label lb = c as Label;
                if (lb != null)
                {
                    lb.Text = dt_Dish_Re.Rows[0]["Y_" + lb.ID.Replace("lbl_", "")].ToString();
                }
            }
        }

        //help
        protected void btn_help_Click(object sender, EventArgs e)
        {
            SetSumNumber();
            this.SelReList();
        }
    }
}